package com.wuqi.dev.mvc.http;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.wuqi.dev.mvc.app.GankApp;
import com.wuqi.dev.mvc.entity.GankEntity;

import java.io.FileNotFoundException;

/**
 * Created by Risky57 on 2017/4/19.
 */

public class HttpManager {

    public static void startRequest(String url, final OnRequestFinishedListener listener){

        RequestQueue queue = Volley.newRequestQueue(GankApp.getContext());
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse (String response) {
                Gson gson = new Gson();
                GankEntity gank = gson.fromJson(response, GankEntity.class);
                if (!gank.isError()){
                    listener.onFinished(gank);
                }else{
                    listener.onFailed(new NullPointerException());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse (VolleyError error) {
                listener.onFailed(error);

            }
        });
        queue.add(request);

    }

    public interface OnRequestFinishedListener{

        void onFinished(GankEntity gank);
        void onFailed(Throwable ex);


    }




}
